package cn.my.framework.activiti;

import cn.my.framework.activiti.modeler.JsonpCallbackFilter;
import org.activiti.engine.IdentityService;
import org.activiti.engine.delegate.event.ActivitiEvent;
import org.activiti.engine.delegate.event.ActivitiEventListener;
import org.activiti.engine.delegate.event.BaseEntityEventListener;
import org.activiti.engine.identity.Group;
import org.activiti.engine.identity.User;
import org.activiti.spring.SpringProcessEngineConfiguration;
import org.activiti.spring.boot.AbstractProcessEngineAutoConfiguration;
import org.activiti.spring.boot.ProcessEngineConfigurationConfigurer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

/**
 * Created by zhangl_lc on 2017/7/18.
 */
@Configuration
public class MyActivitiConfig extends AbstractProcessEngineAutoConfiguration {

    Logger logger = LoggerFactory.getLogger(MyActivitiConfig.class);

    //@Bean
    /*public SpringProcessEngineConfiguration springProcessEngineConfiguration(DataSource dataSource,
            PlatformTransactionManager transactionManager,
            SpringAsyncExecutor springAsyncExecutor) throws IOException {
        return this.baseSpringProcessEngineConfiguration(dataSource, transactionManager, springAsyncExecutor);
    }*/

    @Bean
    public JsonpCallbackFilter filter(){
        return new JsonpCallbackFilter();
    }

    @Component
    public class MyProcessEngineConfigurationConfig implements ProcessEngineConfigurationConfigurer{
        @Override
        public void configure(SpringProcessEngineConfiguration processEngineConfiguration) {
            logger.info("=====<MyProcessEngineConfigurationConfig>=====");
            processEngineConfiguration.setActivityFontName("宋体");
            processEngineConfiguration.setLabelFontName("宋体");
            processEngineConfiguration.setAnnotationFontName("宋体");
            processEngineConfiguration.setJdbcDriver("org.h2.Driver");
            processEngineConfiguration.setJdbcUrl("jdbc:h2:mem:activiti");
            processEngineConfiguration.setJdbcUsername("sa");
            processEngineConfiguration.setJdbcPassword("");
        }
    }


    /*@Bean(name = "processEngineConfiguration")
    public ProcessEngineConfiguration getProcessEngineConfiguration(){
        logger.info("----- init The Process Engine Configuration -----");
        ProcessEngineConfigurationConfigurer
        StandaloneInMemProcessEngineConfiguration pec = new StandaloneInMemProcessEngineConfiguration(){};
        *//*ProcessEngineConfiguration pec = new ProcessEngineConfigurationImpl() {
            @Override
            protected CommandInterceptor createTransactionInterceptor() {
                return null;
            }
        };*//*
        pec.setActivityFontName("微软雅黑");
        pec.setLabelFontName("微软雅黑");
        pec.setEventListeners(new ArrayList<ActivitiEventListener>(){{add(getActivityListener());}});
        Map<String,List<ActivitiEventListener>> typedMap = new HashMap<>();
        List<ActivitiEventListener> typedListener = new ArrayList<>();
        typedListener.add(new MyActivitiEventListener());
        typedMap.put("JOB_EXECUTION_SUCCESS,JOB_EXECUTION_FAILURE",typedListener);
        pec.setTypedEventListeners(typedMap);
        return pec;
    }*/

    @Bean
    public ActivitiEventListener getActivityListener(){
        return new MyActivitiEventListener();
    }

    public class MyActivitiEventListener implements ActivitiEventListener{
        @Override
        public void onEvent(ActivitiEvent event) {
            System.out.println("====== eventType="+event.getType()+",executionId="+event.getExecutionId()+
                    ",processDefinitionId="+event.getProcessDefinitionId()+",processInstanceId="+event.getProcessInstanceId());
        }

        @Override
        public boolean isFailOnException() {
            return false;
        }
    }

    public class MyBaseEntityEventListener extends BaseEntityEventListener{
        @Override
        protected void onCreate(ActivitiEvent event) {
            super.onCreate(event);
            System.out.println(">>>>> onCreate >>> "+event.getType()+" executionId = "+event.getExecutionId());
        }

        @Override
        protected void onDelete(ActivitiEvent event) {
            super.onDelete(event);
            System.out.println(">>>>> onDelete >>> "+event.getType()+" executionId = "+event.getExecutionId());
        }

        @Override
        protected void onUpdate(ActivitiEvent event) {
            super.onUpdate(event);
            System.out.println(">>>>> onUpdate >>> "+event.getType()+" executionId = "+event.getExecutionId());
        }

        @Override
        protected void onEntityEvent(ActivitiEvent event) {
            super.onEntityEvent(event);
            System.out.println(">>>>> onEntityEvent >>> "+event.getType()+" executionId = "+event.getExecutionId());
        }
    }

    //@Bean
    InitializingBean usersAndGroupsInitializer(final IdentityService identityService) {
        return new InitializingBean() {
            public void afterPropertiesSet() throws Exception {
                Group group = identityService.newGroup("user");
                group.setName("users");
                group.setType("security-role");
                identityService.saveGroup(group);

                User admin = identityService.newUser("admin");
                admin.setPassword("admin");
                identityService.saveUser(admin);

            }
        };
    }
}
